<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    :rules="rules"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input v-model="userInfo.password" type="password" style="width:300px" disabled />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="hSumbit">更新</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getUserInfoById, saveUserDetailById } from '@/api/user'
export default {

  data() {
    return {
      // userId: this.$route.params.id
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ]
      },
      userId: this.$route.params.id,
      userInfo: {
        username: '',
        password: ''
      }
    }
  },
  created() {
    this.loadUserInfoById()
  },
  methods: {
    async loadUserInfoById() {
      try {
        const res = await getUserInfoById(this.$route.params.id)
        this.userInfo = res.data
      } catch (error) {
        console.log(error)
      }
    },
    async hSumbit() {
      try {
        const res = await saveUserDetailById(this.userInfo)
        console.log(res)
        this.$router.back()// 默认返回上一级
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

